package cn.hj.pet.controller;

import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.query.BaseQuery;
import cn.hj.basic.util.AjaxResult;
import cn.hj.basic.util.DistanceUtil;
import cn.hj.basic.util.LoginContext;
import cn.hj.basic.util.PageList;
import cn.hj.pet.domain.Pet;
import cn.hj.pet.query.PetQuery;
import cn.hj.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 领养宠物
     * @param id
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id")Long id,HttpServletRequest request){
        try {
            petService.adopt(id,LoginContext.getCurrentLogininfo(request));
            return AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("系统错误，请稍后重试").setSuccess(false);
        }
    }

    /**
     * 查询宠物信息加详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet fandById(@PathVariable("id") Long id){
            return petService.fandById(id);
    }


    /**
     * 分页查询加高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.selectData(query);
    }

    /**
     * 上架宠物
     * @param ids
     * @return
     */
    @PostMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.newAjaxResult();
        } catch (BusinessException e){
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("系统错误，请稍后重试").setSuccess(false);
        }
    }


    /**
     * 下架宠物
     * @param ids
     * @return
     */
    @PostMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.newAjaxResult();
        } catch (BusinessException e){
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("系统错误，请稍后重试").setSuccess(false);
        }
    }
}
